# coding:utf-8
from rest_framework import serializers
from .models import Cart


class CartSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cart
        fields = ('buy_num', 'buyer', 'product', 'price')
        read_only_fields = ('buyer',)

    def create(self, validated_data):
        # 将 `buyer` 设置为当前用户
        validated_data['buyer'] = self.context['request'].user
        return super(CartSerializer, self).create(validated_data)
